﻿using Demo.Models.Entity;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Demo.Controllers
{
    public class TinhHuyenController : Controller
    {
        //
        // GET: /TinhHuyen/




        readonly List<TinhVM> lstTinhVms = new List<TinhVM>();
        readonly List<HuyenVM> lstHuyenVms = new List<HuyenVM>();
        readonly List<SelectListItem> ListTinh = new List<SelectListItem>();

        public TinhHuyenController()
        {
            lstTinhVms.Add(new TinhVM {Id = 1, Name = "Hà Nội"});
            lstTinhVms.Add(new TinhVM { Id = 2, Name = "Hải Dương" });
            lstTinhVms.Add(new TinhVM { Id = 3, Name = "Thái Bình" });

            lstHuyenVms.Add(new HuyenVM{Id = 1, Name = "Cầu Giấy", TinhId = 1});
            lstHuyenVms.Add(new HuyenVM { Id = 2, Name = "Ba Đình", TinhId = 1 });
            lstHuyenVms.Add(new HuyenVM { Id = 3, Name = "Đống Đa", TinhId = 1 });
            lstHuyenVms.Add(new HuyenVM { Id = 4, Name = "Gia Lâm", TinhId = 1 });

            lstHuyenVms.Add(new HuyenVM { Id = 8, Name = "Nam Sách", TinhId = 2 });
            lstHuyenVms.Add(new HuyenVM { Id = 9, Name = "Tiền Trung", TinhId = 2 });
            lstHuyenVms.Add(new HuyenVM { Id = 10, Name = "Gia Lộc", TinhId = 2 });
            lstHuyenVms.Add(new HuyenVM { Id = 11, Name = "Tứ Kỳ", TinhId = 2 });

            lstHuyenVms.Add(new HuyenVM { Id = 5, Name = "Thái Thụy", TinhId = 3 });
            lstHuyenVms.Add(new HuyenVM { Id = 6, Name = "Đông Hưng", TinhId = 3 });
            lstHuyenVms.Add(new HuyenVM {Id = 7, Name = "Tiền Hải", TinhId = 3});

            foreach (var item in lstTinhVms)
            {
                ListTinh.Add(new SelectListItem
                {
                    Text = item.Name,
                    Value = item.Id.ToString(CultureInfo.InvariantCulture)
                });
            }
        }
        public ActionResult Index()
        {
            ViewBag.ListTinh = ListTinh;
            return View();
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetHuyenByTinh(int idTinh)
        {
            var lstHuyen = lstHuyenVms.Where(x => x.TinhId == idTinh).ToList();
            return Json(new { foo = lstHuyen }, JsonRequestBehavior.AllowGet);
        }
	}
}